package com.kexun.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kexun.annotation.LoginManage;
import com.kexun.common.PageResult;
import com.kexun.common.utils.ResponseEntity;
import com.kexun.entity.DO.Department;
import com.kexun.entity.DO.Manage;
import com.kexun.entity.DO.Role;
import com.kexun.service.DepartmentService;
import com.kexun.service.ManageService;
import com.kexun.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/manage")
public class ManageController {


    @Autowired
    private ManageService manageService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private DepartmentService departmentService;


    @GetMapping("list")
    @RequiresPermissions(value = "system/manage/list")
    public String list() {
        return "manage/manage_list";
    }

    @ResponseBody
    @PostMapping("list")
    @RequiresPermissions(value = "system/manage/list")
    public PageResult list(String name, int page, int limit) {
        IPage<Map<String, Object>> pageResult = manageService.findList(name, page, limit);
        return new PageResult(pageResult.getRecords(), pageResult.getTotal());
    }

    @GetMapping("add")
    @RequiresPermissions(value = "system/manage/add")
    public String add(Model model, @LoginManage Manage manage) {
        //查询角色
        List<Role> roleList = roleService.roleList();
        model.addAttribute("roleList", roleList);
        int departmentId = manage.getDepartmentId();
        List<Department> departmentList = departmentService.findDeptById(departmentId);
        model.addAttribute("departmentList", departmentList);
        return "manage/manage_add";
    }

    @ResponseBody
    @PostMapping("add")
    @RequiresPermissions(value = "system/manage/add")
    public ResponseEntity add(Manage manage) {
        int have = manageService.have(manage.getManageName());
        if (have > 0) {
            return new ResponseEntity().error("用户名已存在");
        }
        manageService.register(manage);
        return new ResponseEntity().success();
    }

    @ResponseBody
    @RequestMapping("updState")
    @RequiresPermissions(value = "system/manage/updState")
    public ResponseEntity updState(int id, boolean state) {
        int i = manageService.updState(id, state ? 0 : 1);
        return i > 0 ? new ResponseEntity().success() : new ResponseEntity().error("系统异常!");
    }


    @ResponseBody
    @RequestMapping("del")
    @RequiresPermissions(value = "system/manage/del")
    public ResponseEntity del(int id, @LoginManage Manage manage) {
        if (manage.getId() == id) {
            return new ResponseEntity().error("无法删除当前登录用户!");
        }
        int i = manageService.delManage(id);
        return i > 0 ? new ResponseEntity().success() : new ResponseEntity().error("用户不存在!");
    }


}
